<?php
/** Copyright 2014 TinPhp Systems, Inc.
 * @author zhangxilian 2013-8-15
 */
if (!defined('IN_TINPHP')) {
    exit('Access Denied');
}

require_once('adminApp.php');

class ArticleController extends AdminAppController {
	
	public function __construct() {
		$this->chkLoginType='html';
        parent::__construct();
        $this->artLogic = $this->Logic('ArticleLogic');
	}
 	
 	public function actionDefault(){

 	}
    public function actionLeft(){
        $cats = array();
        try{
            $cats =$this->artLogic->getCatAll("*",'','');
        }catch(Exception $e){
            echo $e->getMessage();
        }
       // $catlist = $this->listcls($cats,0,0,1);
        T::$tpl->set("list", $cats);
    }
    public function actionCats(){
        $cats = array();
        try{
            $cats =$this->artLogic->getCatAll("*",'','');
        }catch(Exception $e){
            echo $e->getMessage();
        }
        $catlist = $this->listcls($cats);
        T::$tpl->set("list", $catlist);
    }
    private function listcls($data,$pid = 0,$level = 0,$istree=0){ //将多维数组格式化为二维数组 └├
        static $val = array();
        global $lang;
        if(is_array($data)){
            if($istree==0){
                $l = str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;",$level);
                $l = $l.""; 
            }else{
            }
            foreach($data as $d){
                if($d['PARENT_ID'] == $pid){         
                    if($istree==1){
                        if($d['ID'] != "") $val[] = array("ID" => $d['ID'],"PARENT_ID" => $d['PARENT_ID'],"SORT_NUM" => $d['SORT_NUM'],"STATE" => $d['STATE'],"NAME" => $d['NAME'],'LEVEL' => $level);
                         $this->listcls($data,$d['ID'],$level+1); //递归调用格式化数组
                     }else{
                        if($d['ID'] != "") $val[] = array("ID" => $d['ID'],"PARENT_ID" => $d['PARENT_ID'],"SORT_NUM" => $d['SORT_NUM'],"STATE" => $d['STATE'],"NAME" => $l.$d['NAME'],'LEVEL' => $level);
                         $this->listcls($data,$d['ID'],$level+1); //递归调用格式化数组
                     }
                }
            }
            return $val;
        }
        return false;
    }
    /** 分类 添加 */
    public function actionCatAdd(){
        T::$tplFile = "admin/article/catact";
        $cats =$this->artLogic->getCatAll("*",'','');
        $mainMenus = $this->listcls($cats,0,0,1);
        T::$tpl->set("mainMenus", $mainMenus);
        if($_POST['actname']=='save'){
            $this->SaveCatData($_POST, 'add');
        }
    }
    /** 分类 修改 */
    public function actionCatEdit(){
        T::$tplFile = "admin/article/catact";        
        $id = $_REQUEST['id'];
        if($id){            
            if($_POST['actname']=='save'){
                $this->SaveCatData($_POST, 'edit');
            }else{
                $cats =$this->artLogic->getCatAll("*",'parent_id!='.$id.' and id!='.$id,'');
                $mainMenus = $this->listcls($cats,0,0,1);
                T::$tpl->set("mainMenus", $mainMenus);
                //显示
                $catItem = $this->artLogic->getCatItem("*","ID=".$id);
                T::$tpl->set('menuItem', $catItem);
            }
        }else{
            $this->sysMessage("修改失败,要修改的ID异常");
        }       
    }  
    /** 分类 保存添加、修改 */
    private function SaveCatData($data, $type){
        if($type=='add'){
            $res = $this->artLogic->addCatItem($data);
            $catid = $res;
        }elseif($type=='edit'){
            $res = $this->artLogic->updateCatItem($data, "ID=".$data['id']);
            $catid = $data['id'];
        }else{
            $this->sysMessage("未知操作..");
        }
        //更新栏目结构
        $cats =$this->artLogic->getCatAll("*",'','');
        foreach($cats as  $cat) {
            $arrchildid = $this->get_arrchildid($cat['ID']);
            $arrparentid =  $this->get_arrparentid($cat['ID']);
            $child = is_numeric($arrchildid) ? 0 : 1;
            if($cat['arrparentid']!=$arrparentid || $cat['arrchildid']!=$arrchildid || $cat['child']!=$child){
               $reset_res = $this->artLogic->updateCatItem(array('arrparentid'=>$arrparentid,'arrchildid'=>$arrchildid,'child'=>$child), "ID=".$cat['ID']);
            }
        }

        if($res && $reset_res){
            $this->sysMessage("修改成功");
        }else{
            $this->sysMessage("修改失败,请重试");
        }
    }
    private function get_arrchildid($catid) {
        $arrchildid = $catid;
        $cats =$this->artLogic->getCatAll("*",'','');
        foreach($cats as  $cat) {
            if($cat['PARENT_ID'] && $cat['PARENT_ID']==$catid) {
                $arrchildid .= ','.$this->get_arrchildid($cat['ID']);
            }
        }
        return $arrchildid;
    }
    private function get_arrparentid($catid, $arrparentid = '', $n = 1) {
        $cats =$this->artLogic->getCatItem("*","ID=".$catid);
        if($n > 5 || !is_array($cats)) return false;
        $parentid = $cats['PARENT_ID'];
        $arrparentid = $arrparentid ? $parentid.','.$arrparentid : $parentid;
        if($parentid) {
            $arrparentid = $this->get_arrparentid($parentid, $arrparentid, ++$n);
        } else {
            $cats['arrparentid'] = $arrparentid;
        }
        $parentid = $cats['PARENT_ID'];
        return $arrparentid;
    }
    /** 删除分类 */
    public function actionCatDel(){
       T::$tplFile='public/empty';
        $id = intval($_REQUEST['id']);
        if($id){
            $this->artLogic->delCatItem("ID=".$id);
            $this->artLogic->delCatItem("PARENT_ID=".$id);
            return $this->sysMessage("操作成功",array("返回分类页"=>"admin.php?ctl=article&act=cats"), 3);
        }else{
            $this->sysMessage("删除失败,ID异常");
        }
    }  
    public function actionList(){
        $page = intval($_REQUEST["p"]) ? $_REQUEST["p"] : 1;
        $catid = intval($_REQUEST["id"]) ? $_REQUEST["id"] : 1;
        $size = 10;
        $arrchildid = $this->get_arrchildid($catid); 

        $res = array();
        try{
            $res = $this->artLogic->getArtList("*", "CATID IN(".$arrchildid.")", "order by id desc", $page, $size);
            if($res['data']){
                foreach($res['data'] as $k=>&$v){
                    $v['cat'] =  $this->artLogic->getCatItem("*","ID=".$v['CATID']);
                }
            }
            $par = array ();
            $res['page'] = page :: listPage($par, $res["count"], $page, $size);
        }catch(Exception $e){
            echo $e->getMessage();
        }
        T::$tpl->set("res", $res);
    }
    /** 文章 添加 */
    public function actionAdd(){
        T::$tplFile = "admin/article/act";
        $cats =$this->artLogic->getCatAll("*",'','');
        $mainMenus = $this->listcls($cats,0,0,1);
        $id = $_REQUEST['id'];
        $Item['CATID']= $id;
        T::$tpl->set('Item', $Item);
        T::$tpl->set("mainMenus", $mainMenus);
        if($_POST['actname']=='save'){
            $this->SaveData($_POST, 'add');
        }
    }
    /** 文章 修改 */
    public function actionEdit(){
        T::$tplFile = "admin/article/act";         
        $id = $_REQUEST['id'];
        if($id){            
            if($_POST['actname']=='save'){
                $this->SaveData($_POST, 'edit');
            }else{
                $cats =$this->artLogic->getCatAll("*",'','');
                $mainMenus = $this->listcls($cats,0,0,1);
                T::$tpl->set("mainMenus", $mainMenus);
                //显示
                $artItem = $this->artLogic->getArtItem("*","ID=".$id);
                T::$tpl->set('Item', $artItem);
            }
        }else{
            $this->sysMessage("修改失败,要修改的ID异常");
        }       
    }   
    /** 文章 保存添加、修改 */
    private function SaveData($data, $type){
        if($type=='add'){
            $res = $this->artLogic->addArtItem($data);
        }elseif($type=='edit'){
            $res = $this->artLogic->updateArtItem($data, "ID=".$data['id']);
        }else{
            $this->sysMessage("未知操作..");
        }
        if($res){
            $this->sysMessage("操作成功",array("返回列表页"=>"admin.php?ctl=article&act=list&id=".$data['catid']),3);
        }else{
            $this->sysMessage("操作失败,请重试");
        }

    }

    /** 文章 删除 */
    public function actionDel(){
       T::$tplFile='public/empty';
        $id = intval($_REQUEST['id']);
        if($id){
            $artItem = $this->artLogic->getArtItem("*","ID=".$id);
            $this->artLogic->delArtItem("ID=".$id);
            return $this->sysMessage("操作成功",array("返回列表页"=>"admin.php?ctl=article&act=list&id=".$artItem['CATID']),3);
        }else{
            $this->sysMessage("删除失败,ID异常");
        }
    }   
}//End Class